﻿using ConfExpert.Forms.Base;
using DevExpress.XtraPivotGrid;
using DevTeam;

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Planlama
{
    public partial class UretimPlanlama : XForm
    {
        public UretimPlanlama()
        {
            InitializeComponent();
        }

        BindingSource bs, bs2;
        DataSet d;
        private void UretimPlanlama_Load(object sender, EventArgs e)
        {
            
            d = ExecuteDataSet("GetUP");
            //d.Relations.Add(1 ile 2 arasına)
            d.Relations.Add("rel1", d.Tables[0].Columns["CustomerId"], d.Tables[1].Columns["CustomerId"]);

            bs = gridView1.Init(d.Tables[0]);
            gridView1.FormatAsNumber(0, "Toplam");
            gridView1.AddSummary("Toplam");
            gridView1.BestFitColumns();

            bs2 = new BindingSource(bs, "rel1");
            InitPivot(pivotGridControl1, bs2);
            pivotGridControl1.BestFit();
            DrillDownGrid(gridView2, bs2);
            gridView2.MakeColumnInvisible("UPCalendarId", "CustomerId");
            gridView2.FormatAsNumber(0, "Adet");
            gridView2.AddSummary("Adet");
            gridView2.BestFitColumns();

            btnKaydet.ItemClick += btnKaydet_ItemClick;
            btnSiparisEkle.ItemClick += btnSiparisEkle_ItemClick;

            splitContainerControl1.AutoSize();
            splitContainerControl2.AutoSize();
        }

        void btnSiparisEkle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            throw new NotImplementedException();
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RefreshData();
        }

        void pivotGridControl1_CustomAppearance(object sender, PivotCustomAppearanceEventArgs e)
        {
            if (e.Focused)
                e.Appearance.BackColor = e.Appearance.BackColor2 = Color.YellowGreen;   // Color.FromArgb(255, 192, 0);
        }


        void RefreshData()
        {
            gridView1.EndEditing();
            gridView2.EndEditing();
            gridView1.RefreshDataSource("GetUP");

            //DrillDownGrid(gridView1, bs);
            //gridView1.AddSummary("Qty");
            //gridView1.OptionsBehavior.Editable = false;

            InitPivot(pivotGridControl1, bs);

            //GetDetails("xOrdine", "Get7cOrdine", CKBatchId);
            //gridView1.Columns["idx"].Visible = 
            //gridView1.Columns["CodCliente"].Visible = false;
            pivotGridControl1.Cells.Selection = new Rectangle(new Point(0, 0), new Size(1, 1));
            //pivotGridControl1.Fields["ProductId"].BestFit();
            pivotGridControl1.BestFit();

        }

        void InitPivot(PivotGridControl pgc, BindingSource bs)
        {
            pgc.DataSource = bs;

            pgc.OptionsCustomization.CustomizationFormStyle = DevExpress.XtraPivotGrid.Customization.CustomizationFormStyle.Excel2007;
            pgc.CellClick += new PivotCellEventHandler(pivotGridControl1_CellClick);
            pgc.CellSelectionChanged += new EventHandler(pgc_CellSelectionChanged);
            pgc.FocusedCellChanged += new EventHandler(pgc_FocusedCellChanged);
            pgc.FieldValueDisplayText += new PivotFieldDisplayTextEventHandler(pivotGrid_FieldValueDisplayText);
            pgc.CustomAppearance += new PivotCustomAppearanceEventHandler(pivotGridControl1_CustomAppearance);


            pgc.BeginUpdate();

            //new field - toplam tutar  (metre x fiyat+dvz cinsi)

            //AddNewField(pgc, "DosyaNo", PivotArea.FilterArea);
            //AddNewField(pgc, "CodCliente", PivotArea.FilterArea);
            //AddNewField(pgc, "Linea", PivotArea.FilterArea);
            /*
            PivotGridFieldBase[] pgf1 = new PivotGridFieldBase[3];
            pgf1[0] = AddNewField(pgc, "Linea", PivotArea.RowArea);
            pgf1[1] = AddNewField(pgc, "Modello", PivotArea.RowArea);
            pgf1[1].ExpandedInFieldsGroup = false;
            pgf1[2] = AddNewField(pgc, "Tex", PivotArea.RowArea);
            pgc.Groups.Add(pgf1, "ModelloTex");
            */

            PivotGridField qa = AddNewField(pgc, "Adet", PivotArea.DataArea);
            qa.ValueFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            qa.ValueFormat.FormatString = "#,###.0";
            qa.Appearance.Value.Options.UseTextOptions = true;
            qa.Appearance.Value.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;

            //AddNewField(pgc, "ProductId", PivotArea.RowArea).Visible = false;   //.FieldEdit = RI(Rp.V_Kumas);
            AddNewField(pgc, "CustomerId", PivotArea.RowArea).FieldEdit = RI(Rp.V_Customer);
            //AddNewField(pgc, "Ay", PivotArea.ColumnArea);
            qa = AddNewField(pgc, "Ay", PivotArea.ColumnArea);
            qa.ValueFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            qa.ValueFormat.FormatString = "MM/yyyy";
            pgc.EndUpdate();
        }


        void pgc_FocusedCellChanged(object sender, EventArgs e)
        {
            CellChanged();
        }

        void pgc_CellSelectionChanged(object sender, EventArgs e)
        {
            CellChanged();
        }

        private void CellChanged()
        {
            double PivotSum = 0.0;

            if (pivotGridControl1.Cells.MultiSelection.SelectedCells.Count > 0)
            {
                foreach (Point cell in pivotGridControl1.Cells.MultiSelection.SelectedCells)
                {
                    DevExpress.XtraPivotGrid.PivotCellEventArgs cellInfo = pivotGridControl1.Cells.GetCellInfo(cell.X, cell.Y);
                    if (cellInfo.ColumnValueType == DevExpress.XtraPivotGrid.PivotGridValueType.Value &&
                        cellInfo.RowValueType == DevExpress.XtraPivotGrid.PivotGridValueType.Value)
                        PivotSum += Convert.ToDouble(pivotGridControl1.GetCellValue(cell.X, cell.Y));
                }
            }
            else
            {
                DevExpress.XtraPivotGrid.PivotCellEventArgs cellInfo = pivotGridControl1.Cells.GetFocusedCellInfo();
                if (cellInfo.ColumnValueType == DevExpress.XtraPivotGrid.PivotGridValueType.Value &&
                    cellInfo.RowValueType == DevExpress.XtraPivotGrid.PivotGridValueType.Value)
                    PivotSum += Convert.ToDouble(pivotGridControl1.GetCellValue(pivotGridControl1.Cells.FocusedCell.X, pivotGridControl1.Cells.FocusedCell.Y));

            }
            Notify(PivotSum == 0 ? string.Empty : String.Format("{0:#,##0.## mt'.'}", PivotSum));
        }

        void pivotGridControl1_CellClick(object sender, DevExpress.XtraPivotGrid.PivotCellEventArgs e)
        {
            gridControl2.DataSource = e.CreateDrillDownDataSource();
            gridView2.MakeColumnInvisible("UPCalendarId", "CustomerId");
            ExpandCollapse(splitContainerControl1, (gridView2.RowCount > 0 ? AcKapa.Expand : AcKapa.Collapse));
            gridView2.BestFitColumns();
            gridView2.MoveFirst();
        }
    }
}
